package com.example.demo.bizhan.anotation;

import org.junit.Test;

import java.lang.reflect.Field;

import static java.lang.System.*;

/**
 * @description: xxx
 * @time: 2020/10/27 14:51
 * @author: baojinlong
 **/
public class AnimalDemoTest {

    @Test
    public void test02() throws Exception {
        Class<?> animalClazz = Class.forName("com.example.demo.bizhan.anotation.Animal");
        Field[] declaredFields = animalClazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            out.println(declaredField);
            PkField annotation = declaredField.getAnnotation(PkField.class);
            if (annotation != null) {
                out.println(annotation.value());
            }
            boolean annotationPresent = declaredField.isAnnotationPresent(PkField.class);
            if (annotationPresent) {
                out.println(declaredField + " 有注解");
            }
        }

    }

    @Test
    public void test01() throws Exception {
        Class<?> animalClazz = Class.forName("com.example.demo.bizhan.anotation.Animal");
        boolean annotation = animalClazz.isAnnotationPresent(PkBean.class);
        if (annotation) {
            // 如果存在PkBean这个注解
            PkBean pkBeanAnnotation = animalClazz.getAnnotation(PkBean.class);
            if (pkBeanAnnotation != null) {
                out.println(pkBeanAnnotation);
                out.println(pkBeanAnnotation.table() + " : " + pkBeanAnnotation.from());
            }
        }

    }
}
